import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import VueAxios from 'vue-axios'
// 导入全局过滤器
import filters from './filters'

Object.keys(filters).forEach(k => Vue.filter(k, filters[k]));

// 设置全局axios请求头
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';
// 设置axios 拦截器
axios.interceptors.request.use(
  config =>{
    if (store.state.Authorization){
      config.headers.Authorization = `${store.state.Authorization}`;
    };
    return config
  },
  err =>{
    return Promise.reject(err)
  })

axios.interceptors.response.use(
  response =>{
    return response;
  },
  error=>{
    if(error.response){
      switch (error.response.status){
        case 401:
        // 返回401的时候 清楚token信息
          // store.commit('logout');
          router.replace({
            path:'/login',
            query:{redirect: router.currentRoute.fullPath}
          })
      }
    }
    return Promise.reject(error.response.data)   // 返回接口返回的错误信息
  });
// import global_ from './common/enum'
// Vue.prototype.GLOBAL = global_




//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  mode:'history',
  routes
})

router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.meta.requireAuth) {
    if(store.state.Authorization){
      next();
    }
    else{
      next({
        path:'/login',
        query:{
          redirect: to.fullPath}
      })
    }
  }
  else{
    next();
  }

  if (to.path == '/logout') {
    store.commit('changeLogout');
    next({path : '/login'});
  }

 
})

//router.afterEach(transition => {
//NProgress.done();
//});

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueAxios, axios)


new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

